package com.codingfuture.glacierhardware.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codingfuture.glaciercommon.util.PageDate;
import com.codingfuture.glaciercommon.util.PageParam;
import com.codingfuture.glaciercommon.util.Result;
import com.codingfuture.glacierhardware.dto.HostDTO;
import com.codingfuture.glacierhardware.entity.Host;
import com.codingfuture.glacierhardware.service.HostService;
import com.codingfuture.glacierhardware.vo.CompanyVO;
import com.codingfuture.glacierhardware.vo.WareHouseVO;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.codingfuture.glaciercommon.constant.ResultConstant.ADD_NO_MSG;
import static com.codingfuture.glaciercommon.constant.ResultConstant.ADD_YES_MSG;

@Api(tags = "主机接口")
@RestController
@RequestMapping("/host")
@CrossOrigin
@RefreshScope
@Slf4j
public class HostController {

    @Autowired
    private HostService hostService;

    /**
     * 查询主机信息
     *
     * @param pageParam 分页
     * @param name      名称
     * @return Result<PageDate < List < Host>>>
     */
    @GetMapping("/getHost")
    @ApiOperation("查询主机信息")
    public Result<PageDate<List<Host>>> getHost(PageParam pageParam, String name) {
        Page<Host> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        hostService.getHost(name, page);
        PageDate<List<Host>> pageDate = new PageDate<>();
        log.debug("getHost{}", page.getRecords());
        pageDate.setRows(page.getRecords());
        pageDate.setTotal(page.getTotal());
        return Result.OK(pageDate);
    }

    /**
     * 查询主机信息
     *
     * @param dto HostDTO
     * @return Result<PageDate < List < Host>>>
     */
    @PostMapping("/addOrUpHost")
    public Result<Void> addOrUpHost(@RequestBody HostDTO dto) {
        return hostService.addOrUpHost(dto) > 0 ? Result.success(ADD_YES_MSG)
                : Result.err(ADD_NO_MSG);
    }

    /**
     * 查询主机信息
     *
     * @param id
     * @return Result<Void>
     */
    @DeleteMapping("/delHost")
    public Result<Void> delHost(@RequestParam Long id) {
        return hostService.delHost(id) > 0 ? Result.success(ADD_YES_MSG)
                : Result.err(ADD_NO_MSG);
    }

    /**
     * 查询企业名称
     *
     * @return Result<List < CompanyVO>>
     */
    @GetMapping("/getCompanyName")
    public Result<List<CompanyVO>> getCompanyName() {
        return Result.OK(hostService.getCompanyName());
    }

    /**
     * 查询仓库名称
     *
     * @return Result<List < WareHouseVO>>
     */
    @GetMapping("/getWarehouseName")
    public Result<List<WareHouseVO>> getWarehouseName(Integer companyId) {
        return Result.OK(hostService.getWarehouseName(companyId));
    }
}
